package com.ksd.modules.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.channel.entity.AdminChannel;
import com.ksd.modules.channel.mapper.AdminChannelMapper;
import com.ksd.modules.channel.po.AdminChannelSavePO;
import com.ksd.modules.channel.po.AdminChannelSearchPO;
import com.ksd.modules.channel.service.IAdminChannelService;
import com.ksd.modules.channel.vo.AdminChannelVo;
import com.ksd.redis.util.RedisUtil_Back;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminChannelServiceImpl extends BaseServiceImpl<AdminChannelMapper,AdminChannel> implements IAdminChannelService {


    @Autowired
    private RedisUtil_Back redisUtilBack;
    private static final String CACHE_CHANNEL_KEY1 = "KSD:SYSTEM:FindChannels:LIST:";
    private static final String CACHE_CHANNEL_KEY2 = "KSD:SYSTEM:FindChannelZones:LIST:";

    /**
     * 查询栏目信息
     * @param adminChannelSearchPO
     * @return
     */
    public Page<AdminChannelVo> findAdminChannels(AdminChannelSearchPO adminChannelSearchPO){
        Page<AdminChannel> page = new Page<>(adminChannelSearchPO.getPageNo(),adminChannelSearchPO.getPageSize());
        LambdaQueryWrapper<AdminChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminChannel::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminChannel::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(!Objects.isNull(adminChannelSearchPO.getStatus()),AdminChannel::getStatus,adminChannelSearchPO.getStatus());
        lambdaQueryWrapper.eq(!Objects.isNull(adminChannelSearchPO.getNewFlag()),AdminChannel::getNewFlag,adminChannelSearchPO.getNewFlag());
        lambdaQueryWrapper.eq(!Objects.isNull(adminChannelSearchPO.getHotFlag()),AdminChannel::getHotFlag,adminChannelSearchPO.getHotFlag());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(adminChannelSearchPO.getKeyword()),AdminChannel::getTitle,adminChannelSearchPO.getKeyword());
        lambdaQueryWrapper.orderByAsc(AdminChannel::getSorted);
        Page<AdminChannel> adminChannelPage = this.page(page, lambdaQueryWrapper);
        Page<AdminChannelVo> adminChannelVoPage = tranferPageBo(adminChannelPage, AdminChannelVo.class);
        return adminChannelVoPage;
    }

    /**
     * 查询栏目回收站信息
     * @param adminChannelSearchPO
     * @return
     */
    public Page<AdminChannelVo> findAdminChannelsRecyle(AdminChannelSearchPO adminChannelSearchPO){
        Page<AdminChannel> page = new Page<>(adminChannelSearchPO.getPageNo(),adminChannelSearchPO.getPageSize());
        LambdaQueryWrapper<AdminChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminChannel::getIsDeleted, DeletedEnums.DELETED.getCode());
        lambdaQueryWrapper.eq(AdminChannel::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(!Objects.isNull(adminChannelSearchPO.getStatus()),AdminChannel::getStatus,adminChannelSearchPO.getStatus());
        lambdaQueryWrapper.eq(!Objects.isNull(adminChannelSearchPO.getNewFlag()),AdminChannel::getNewFlag,adminChannelSearchPO.getNewFlag());
        lambdaQueryWrapper.eq(!Objects.isNull(adminChannelSearchPO.getHotFlag()),AdminChannel::getHotFlag,adminChannelSearchPO.getHotFlag());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(adminChannelSearchPO.getKeyword()),AdminChannel::getTitle,adminChannelSearchPO.getKeyword());
        lambdaQueryWrapper.orderByAsc(AdminChannel::getSorted);
        Page<AdminChannel> adminChannelPage = this.page(page, lambdaQueryWrapper);
        Page<AdminChannelVo> adminChannelVoPage = tranferPageBo(adminChannelPage, AdminChannelVo.class);
        return adminChannelVoPage;
    }

    /**
     * 保存栏目和修改栏目
     * @param adminChannelSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminChannelVo saveUpdateAdminChannel(AdminChannelSavePO adminChannelSavePO){
        AdminChannel adminChannel = tranferBo(adminChannelSavePO, AdminChannel.class);
        adminChannel.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminChannel.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminChannel);
        if(b){
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
            AdminChannelVo adminChannelVo = tranferBo(adminChannel, AdminChannelVo.class);
            return adminChannelVo;
        }
        return null;
    }

    /**
     * 获取栏目明细
     * @param id
     * @return
     */
    public AdminChannelVo getAdminChannelById(Long id){
        AdminChannel adminChannel = this.getById(id);
        AdminChannelVo adminChannelVo = tranferBo(adminChannel, AdminChannelVo.class);
        return adminChannelVo;
    }

    /**
     * 单个逻辑删除
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean deleteAdminChannelById(Long id){
        boolean b =  this.updateStatus(id,AdminChannel.Columns.IS_DELETED,1);
        if(b) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 单个恢复
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean recoverByIdAdminChannel(Long id){
        boolean b =  this.updateStatus(id,AdminChannel.Columns.IS_DELETED,0);
        if(b) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminChannelById(Long id){
        boolean b = this.removeById(id);
        if(b) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 批量逻辑删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean bacthDeletesAdminChannel(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminChannel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return true;
    }

    /**
     * 批量恢复
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRecoversAdminChannel(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminChannel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.UN_DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if (b) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminChannel(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }


    /**
     * 覆盖方法
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        if(b) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 覆盖方法
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        if(b) {
            redisUtilBack.del(CACHE_CHANNEL_KEY1 + SystemIdThrealLocal.get().getSystemId());
            redisUtilBack.del(CACHE_CHANNEL_KEY2 + SystemIdThrealLocal.get().getSystemId());
        }
        return b;
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminChannel data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
